package com.example.chat.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.*;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.VideoMapper;
import com.example.chat.mapper.VideoTypeMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.result.ListResult;
import com.example.chat.result.VideoResult;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.Global;
import com.example.chat.utils.OssUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class VideoTypeService extends ServiceImpl<VideoTypeMapper, VideoType> {
    @Resource
    CheckUtils checkUtils;
    @Resource
    AdminMapper adminMapper;
    @Resource
    Global global;
    @Resource
    OssUtil ossUtil;


    public ApiResult<Base> addVideoType(Integer userId, String token, String name, int sort) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "");
        if (apiResult.getStatus() != 1){
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);}
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "创建失败";
        try {
            VideoType videoType = new VideoType();
            videoType.setName(name);
            videoType.setSort(sort);
            videoType.setParentId(0);
            videoType.setIsDelete(0);
            videoType.setCreateTime(new Date());
            baseMapper.insert(videoType);
            status = 1;
            msg = "创建成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> updateVideoType(Integer userId, String token, long id, int isDelete, String name, int sort) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "");
        if (apiResult.getStatus() != 1){
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);}
        VideoType videoType = baseMapper.selectById(id);
        if (null != videoType) {
            ApiResult<Base> result = new ApiResult<>();
            int status = 1;
            String msg = "修改成功";
            if (isDelete == 1) {
                msg = "删除成功";
                videoType.setIsDelete(isDelete);
            } else {
                videoType.setSort(sort);
                videoType.setName(name);
            }
            updateById(videoType);
            result.setStatus(status);
            result.setMsg(msg);
            return result;
        } else {
            return new ApiResult<>(0, "视频类型不存在!", null);
        }
    }
}
